<?php

namespace App\Models\Press;

use App\Models\Products\Product;
use App\Models\Store\Store;
use App\Services\Locale\Language;
use App\Models\Language as Language_model;
use App\Services\MobileApp\AppEnv;
use App\Services\Utils\ImageHelper;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

/**
 * @property Carbon date_added
 * @property Carbon date_modified
 *
 * @method static self filterStoreId($store_id)
 * @method static self filterPressCategoryId($press_category_id)
 * @method static self filterProductId($product_id)
 *
 * @property-read string $time_ago
 * @property string title
 * @property PressDescription filterPressDescriptionByLanguage
 * @property Product[]|Collection pressProducts
 *
 * @mixin Builder
 */
class Press extends Model
{
    const PRESS_STATUS_ON = 1; // 状态启用中
    const PRESS_STATUS_OFF = 0; // 状态关闭中

    protected $table = 'press';
    protected $guarded = [];
    protected $appends = [
        'time_ago'
    ];

    protected $primaryKey = 'press_id';

    const CREATED_AT = 'date_added';

    const UPDATED_AT = 'date_modified';
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function pressToStore()
    {
        return $this->hasOne(PressToStore::class, 'press_id', 'press_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function filterPressDescriptionByLanguage()
    {
        return $this->hasOne(PressDescription::class, 'press_id', 'press_id')
            ->where('language_id', AppEnv::languageId());
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function pressCategory()
    {
        return $this->hasOne(PressCategory::class, 'press_id', 'press_id');
    }

    /**
     * deprecated (废弃，改用pressRelateds)
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
     */
    public function productRelateds()
    {
        return $this->hasManyThrough(
            ProductRelated::class,
            PressProduct::class,
            'press_id',
            'related_id',
            'press_id',
            'related_id'
        );
    }

    /**
     * @used-by $pressProducts
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function pressProducts()
    {
        return $this->hasMany(PressProduct::class, 'press_id', 'press_id');
    }

    /**
     * @return null|string
     */
    public function getImageAttribute()
    {
        $route = $this->attributes['image'];

        if ($route) {
            return ImageHelper::generateImagePath($route);
        } else {
            return null;
        }
    }

    /**
     * @used-by $time_ago
     *
     * @return string
     */
    public function getTimeAgoAttribute()
    {
        // Carbon 的 Locale 参数只取前两个字符
        $language_name = app()->getLocale();
        Carbon::setLocale(head(explode('-', $language_name)));

        if ($this->date_added->addDay(10) < now()) {
            return $this->attributes['date_added'];
        } else {
            return $this->date_added->diffForHumans();
        }
    }

    /**
     * @used-by filterStoreId
     *
     * @param Builder $query
     * @param $store_id
     * @return Builder
     */
    public function scopeFilterStoreId(Builder $query, $store_id)
    {
        if (! $store_id) return $query;

        return $query->whereHas('pressToStore', function(Builder $query) use($store_id) {
            return $query->where('store_id', $store_id);
        });
    }

    /**
     * @used-by filterPressCategoryId
     *
     * @param Builder $query
     * @param $press_category_id
     * @return Builder
     */
    public function scopeFilterPressCategoryId(Builder $query, $press_category_id)
    {
        if (! $press_category_id) return $query;

        return $query->whereHas('pressCategory', function(Builder $query) use($press_category_id) {
            return $query->where('press_category_id', $press_category_id);
        });
    }

    /**
     * @used-by filterProductId
     *
     * @param Builder $query
     * @param $product_id
     * @return Builder
     */
    public function scopeFilterProductId(Builder $query, $product_id)
    {
        if (! $product_id) return $query;

        return $query->whereHas('productRelateds', function(Builder $query) use($product_id) {
            return $query->where('product_id', $product_id);
        });
    }
}
